Introductory example
Info: this guide gives you an overview of the typical optimization workflow with mrMBO. For a much more detailed introduction see the next chapter.
Here we provide a quickstart example for you to make yourself familiar with mlrMBO. We aim to optimize the one dimensional Rastrigin function using model-based optimization. Instead of writing this function by hand, we make use of the smoof package, which offers a lot of common single objective optimization functions.
library(smoof)
#> Loading required package: checkmate
#>
#> Attaching package: 'smoof'
#> The following object is masked from 'package:mlr':
#>
#> getParamSet
library(mlr)
library(mlrMBO)
#> Loading required package: lhs
#> Warning: replacing previous import by 'smoof::getParamSet' when loading
#> 'mlrMBO'
#>
#> Attaching package: 'mlrMBO'
#> The following object is masked from 'package:ParamHelpers':
#>
#> plotEAF
library(ParamHelpers)
obj.fun = makeRastriginFunction(1)
Note: Since all this stuff here is still under developement it might be neccessary to install the github developement version of the ParamHelpers package via the devtools::install_github function.
We decide to use kriging as our surrogate model and to do 10 sequential optimization steps. Furthermore we use Expected Improvement (EI) as the infill criterion, i. e., the criterion which determines which point(s) of the objective function should be evaluated in each iterations (keep in mind, that using EI as the infill criterion needs the learner to support standard error estimation).
As a last step we have to generate an initial design on which we evaluate our model in the beginning. We use ParamHelpers::generateDesign to generate 10 points in a latin hypercube design.
learner = makeLearner("regr.km", predict.type = "se", covtype = "matern3_2")
control = makeMBOControl()
control = setMBOControlTermination(control, iters = 10)
control = setMBOControlInfill(control, crit = "ei")
design = generateDesign(n = 10, par.set = smoof::getParamSet(obj.fun))
Finally we start the optimization process and print the result object.
result = mbo(obj.fun, design = design, learner = learner, control = control,
show.info = TRUE)
#> Computing y column(s) for design. Not provided.
#> [mbo] 0: x=4.17 : y = 22.8 : 0.0 secs : initdesign
#> [mbo] 0: x=2.74 : y = 18.4 : 0.0 secs : initdesign
#> [mbo] 0: x=0.272 : y = 11.5 : 0.0 secs : initdesign
#> [mbo] 0: x=-1.65 : y = 18.4 : 0.0 secs : initdesign
#> [mbo] 0: x=2.03 : y = 4.32 : 0.0 secs : initdesign
#> [mbo] 0: x=3.11 : y = 12.1 : 0.0 secs : initdesign
#> [mbo] 0: x=-2.44 : y = 25.3 : 0.0 secs : initdesign
#> [mbo] 0: x=-3.89 : y = 17.4 : 0.0 secs : initdesign
#> [mbo] 0: x=-0.769 : y = 9.37 : 0.0 secs : initdesign
#> [mbo] 0: x=-4.37 : y = 35.9 : 0.0 secs : initdesign
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -42.16228
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 42.162 |proj g|= 1.9768
#> At iterate 1 f = 35.839 |proj g|= 0.31573
#> At iterate 2 f = 35.68 |proj g|= 0
#>
#> iterations 2
#> function evaluations 3
#> segments explored during Cauchy searches 2
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 35.6801
#>
#> F = 35.6801
#> final value 35.680121
#> converged
#> [mbo] 1: x=-2.05 : y = 4.7 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -50.85536
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 50.855 |proj g|= 1.8606
#> At iterate 1 f = 39.744 |proj g|= 0
#>
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 39.7438
#>
#> F = 39.7438
#> final value 39.743841
#> converged
#> [mbo] 2: x=3.62 : y = 30.3 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -46.05445
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 46.054 |proj g|= 0.45522
#> At iterate 1 f = 43.926 |proj g|= 0
#>
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 43.9265
#>
#> F = 43.9265
#> final value 43.926485
#> converged
#> [mbo] 3: x=-3.55 : y = 32.2 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -49.18026
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 49.18 |proj g|= 0.30591
#> At iterate 1 f = 48.175 |proj g|= 0
#>
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 48.1752
#>
#> F = 48.1752
#> final value 48.175161
#> converged
#> [mbo] 4: x=3.72 : y = 25.6 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -52.05361
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 52.054 |proj g|= 0.30731
#> At iterate 1 f = 51.605 |proj g|= 0
#>
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 51.6047
#>
#> F = 51.6047
#> final value 51.604695
#> converged
#> [mbo] 5: x=-3.79 : y = 22 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -54.14426
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 54.144 |proj g|= 5.6298
#> At iterate 1 f = 54.011 |proj g|= 0.22682
#> At iterate 2 f = 53.963 |proj g|= 0.1198
#> At iterate 3 f = 53.963 |proj g|= 0.0074116
#> At iterate 4 f = 53.963 |proj g|= 1.392e-05
#> At iterate 5 f = 53.963 |proj g|= 1.5912e-09
#>
#> iterations 5
#> function evaluations 8
#> segments explored during Cauchy searches 5
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 0
#> norm of the final projected gradient 1.59118e-09
#> final function value 53.9628
#>
#> F = 53.9628
#> final value 53.962788
#> converged
#> [mbo] 6: x=1.94 : y = 4.5 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -57.42694
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 57.427 |proj g|= 0.37626
#> At iterate 1 f = 56.944 |proj g|= 0.31386
#> At iterate 2 f = 56.668 |proj g|= 0.24689
#> At iterate 3 f = 56.657 |proj g|= 0.52416
#> At iterate 4 f = 56.656 |proj g|= 0.025438
#> At iterate 5 f = 56.656 |proj g|= 0.00038393
#> At iterate 6 f = 56.656 |proj g|= 2.9003e-07
#>
#> iterations 6
#> function evaluations 9
#> segments explored during Cauchy searches 6
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 0
#> norm of the final projected gradient 2.90031e-07
#> final function value 56.6555
#>
#> F = 56.6555
#> final value 56.655547
#> converged
#> [mbo] 7: x=-1.96 : y = 4.13 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -68.84067
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 68.841 |proj g|= 1.4873
#> At iterate 1 f = 63.315 |proj g|= 0
#>
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 63.3155
#>
#> F = 63.3155
#> final value 63.315486
#> converged
#> [mbo] 8: x=1.13 : y = 4.36 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -64.75254
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 64.753 |proj g|= 0.52075
#> At iterate 1 f = 63.962 |proj g|= 0.43422
#> At iterate 2 f = 63.285 |proj g|= 0.30913
#> At iterate 3 f = 63.269 |proj g|= 1.1478
#> At iterate 4 f = 63.261 |proj g|= 0.074181
#> At iterate 5 f = 63.261 |proj g|= 0.0026487
#> At iterate 6 f = 63.261 |proj g|= 6.4719e-06
#>
#> iterations 6
#> function evaluations 9
#> segments explored during Cauchy searches 6
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 0
#> norm of the final projected gradient 6.47193e-06
#> final function value 63.2613
#>
#> F = 63.2613
#> final value 63.261339
#> converged
#> [mbo] 9: x=1.32 : y = 16 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -76.513
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 76.513 |proj g|= 1.3051
#> At iterate 1 f = 70.586 |proj g|= 0
#>
#> iterations 1
#> function evaluations 2
#> segments explored during Cauchy searches 1
#> BFGS updates skipped 0
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 70.586
#>
#> F = 70.586
#> final value 70.586034
#> converged
#> [mbo] 10: x=1.41 : y = 20.4 : 0.0 secs : infill_ei
#>
#> optimisation start
#> ------------------
#> * estimation method : MLE
#> * optimisation method : BFGS
#> * analytical gradient : used
#> * trend model : ~1
#> * covariance model :
#> - type : matern3_2
#> - nugget : NO
#> - parameters lower bounds : 1e-10
#> - parameters upper bounds : 17.08579
#> - best initial criterion value(s) : -85.50927
#>
#> N = 1, M = 5 machine precision = 2.22045e-16
#> At X0, 0 variables are exactly at the bounds
#> At iterate 0 f= 85.509 |proj g|= 1.4973
#> At iterate 1 f = 82.144 |proj g|= 2.0383
#> ys=-2.885e+00 -gs= 2.242e+00, BFGS update SKIPPED
#> At iterate 2 f = 73.855 |proj g|= 0
#>
#> iterations 2
#> function evaluations 3
#> segments explored during Cauchy searches 2
#> BFGS updates skipped 1
#> active bounds at final generalized Cauchy point 1
#> norm of the final projected gradient 0
#> final function value 73.855
#>
#> F = 73.855
#> final value 73.854960
#> converged
print(result)
#> Recommended parameters:
#> x=-1.96
#> Objective: y = 4.134
#>
#> Optimization path
#> 10 + 10 entries in total, displaying last 10 (or less):
#> x y dob eol error.message exec.time ei
#> 11 -2.050284 4.698625 1 NA <NA> 0.000 -0.2288446
#> 12 3.623820 30.255372 2 NA <NA> 0.000 -0.3726289
#> 13 -3.546339 32.155650 3 NA <NA> 0.001 -0.3422541
#> 14 3.722633 25.569052 4 NA <NA> 0.001 -0.3188126
#> 15 -3.787732 21.998262 5 NA <NA> 0.000 -0.2604270
#> 16 1.938323 4.498631 6 NA <NA> 0.000 -1.1183878
#> 17 -1.961884 4.134397 7 NA <NA> 0.000 -0.9190494
#> 18 1.128445 4.357019 8 NA <NA> 0.001 -0.4194018
#> 19 1.319269 15.956642 9 NA <NA> 0.000 -1.4304011
#> 20 1.409904 20.427879 10 NA <NA> 0.000 -0.4781482
#> error.model train.time prop.type propose.time exec.timestamp se
#> 11 <NA> 0.028 infill_ei 0.361 1464947390 8.576909
#> 12 <NA> 0.017 infill_ei 0.244 1464947390 8.972263
#> 13 <NA> 0.017 infill_ei 0.250 1464947391 9.408390
#> 14 <NA> 0.017 infill_ei 0.367 1464947392 9.843812
#> 15 <NA> 0.017 infill_ei 0.386 1464947392 9.651479
#> 16 <NA> 0.020 infill_ei 0.413 1464947393 5.725409
#> 17 <NA> 0.020 infill_ei 0.265 1464947393 4.622039
#> 18 <NA> 0.017 infill_ei 0.260 1464947394 10.029210
#> 19 <NA> 0.021 infill_ei 0.286 1464947394 7.264680
#> 20 <NA> 0.019 infill_ei 0.267 1464947395 9.935517
#> mean
#> 11 17.538842
#> 12 16.371550
#> 13 17.528535
#> 14 18.653697
#> 15 19.147651
#> 16 7.231362
#> 17 6.618227
#> 18 17.570495
#> 19 7.787929
#> 20 16.790109
Example run
There is also the function exampleRun, which is useful to figure out how mbo works and to visualize the results.
ex = exampleRun(obj.fun, control = control, show.info = FALSE)
#> Warning in makeMboLearner(control, fun): filter.proposed.points is not set
#> in the control object. This might lead to the 'leading minor of order ...'
#> error during model fit.
print(ex)
#> MBOExampleRun
#> Number of parameters : 1
#> Parameter names : x
#> Parameter types : numericvector
#> Global Opt (known) : 0.0000e+00
#> Gap for best point : 9.1272e-04
#> True points per dim. : 50
#> Objectives : 1
#> Points proposed per iter : 1
#>
#> Infill criterion : ei
#> Infill optimizer : focussearch
#> Infill optimizer restarts : 1
#> Final point by : best.true.y
#> Learner : regr.km
#> Learner settings:
#> jitter=FALSE,nugget.stability=1e-08
#> Recommended parameters:
#> x=-0.00214
#> Objective: y = 9.127e-04
plotExampleRun(ex)
Or alternatively for a two dimensional function:
obj.fun2 = makeRastriginFunction(2L)
design2 = generateDesign(n = 10, par.set = smoof::getParamSet(obj.fun2))
ex2 = exampleRun(obj.fun2, control = control, show.info = FALSE)
#> Warning in makeMboLearner(control, fun): filter.proposed.points is not set
#> in the control object. This might lead to the 'leading minor of order ...'
#> error during model fit.
print(ex2)
#> MBOExampleRun
#> Number of parameters : 2
#> Parameter names : x1,x2
#> Parameter types : numericvector
#> Global Opt (known) : 0.0000e+00
#> Gap for best point : 2.5901e+00
#> True points per dim. : 50
#> Objectives : 1
#> Points proposed per iter : 1
#>
#> Infill criterion : ei
#> Infill optimizer : focussearch
#> Infill optimizer restarts : 1
#> Final point by : best.true.y
#> Learner : regr.km
#> Learner settings:
#> jitter=FALSE,nugget.stability=1e-08
#> Recommended parameters:
#> x=0.0525,1.07
#> Objective: y = 2.590e+00
plotExampleRun(ex2)
#> Loading required package: gridExtra